package com.hongdee.scm.saleorder.controller;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.saleorder.entity.TScmSaleOrder;
import com.hongdee.scm.saleorder.service.ITScmSaleOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/5/14.
 */
@RestController
@RequestMapping("/scm/saleOrder")
public class TScmSaleOrderController {
    @Autowired
    private ITScmSaleOrderService itScmSaleOrderService;
    /**
     * 销售单分页查询
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "status", defaultValue = "50") String status,
                    @RequestBody Map data) throws Exception {

        Map map = itScmSaleOrderService.page(data, start, limit, uid,status);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 销售车辆管理
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/pageCar")
    @ResponseBody
    public Map pageCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestBody Map data) throws Exception {

        Map map = itScmSaleOrderService.pageCar(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 销售车辆已出库的车辆
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/outPageCar")
    @ResponseBody
    public Map outPageCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestBody Map data) throws Exception {

        Map map = itScmSaleOrderService.outPageCar(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 销售单保存
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "0") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TScmSaleOrder data) throws Exception {
        this.itScmSaleOrderService.saveOrUpdate(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 一单多车
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveSale")
    @ResponseBody
    public ResultEntity saveSale(@RequestParam(name = "uid", defaultValue = "0") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestBody Map data) throws Exception {
        this.itScmSaleOrderService.saveSaleOrder(data,uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 销售单作废
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/saleOrderCancel")
    @ResponseBody
    public ResultEntity saleOrderCancel(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                                        @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        this.itScmSaleOrderService.saleOrderCancel(uid, id);
        ResultEntity res = new ResultEntity();
        res.setMsg("已作废！");
        res.setSuccess(true);
        return res;
    }
    /**
     * 销售单信息详情
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择销售发票信息！");
        }
        Map map = itScmSaleOrderService.info(id, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    @RequestMapping("/infoCar")
    @ResponseBody
    public ResultEntity infoCar(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择销售发票信息！");
        }
        Map map = itScmSaleOrderService.infoCar(id, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
    /**
     * 销售单配车接口
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/config")
    @ResponseBody
    public ResultEntity config(@RequestParam(name = "uid", defaultValue = "0") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TScmSaleOrder data) throws Exception {
        Map map = this.itScmSaleOrderService.configCar(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("配车成功！");
        res.setSuccess(true);
        res.setData(map);
        return res;
    }

    /**
     * 查询车辆成本（无发票取暂估金额，反之取发票金额）
     * @param uid
     * @param token
     * @param vin
     * @return
     * @throws Exception
     */
    @RequestMapping("/carCost")
    @ResponseBody
    public Map carCost(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        Map map = itScmSaleOrderService.carCost(vin, uid);
        map.put("success", true);
        map.put("msg", "查询成功！");
        return map;
    }

    @RequestMapping("/carCostMore")
    @ResponseBody
    public Map carCostMore(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestBody String[] vins) throws Exception {
        Map map = new HashMap();
        List dataList = itScmSaleOrderService.carCostMore(vins, uid);
        map.put("dataList",dataList);
        map.put("success", true);
        map.put("msg", "查询成功！");
        return map;
    }


    @RequestMapping("/ifCarIn")
    @ResponseBody
    public ResultEntity ifCarIn(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestBody Map data) throws Exception {

        itScmSaleOrderService.ifCarIn(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("刷新车辆成功！");
        return res;
    }

}
